package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.List;

public class MainMenu extends Menu {
    private final static String menuTitle = "Please select an option:";
    private final static String invalidMessage = "Select a valid option!";
    private static List<Service> services = new ArrayList<Service>();
    private List<String> menuOptions = new ArrayList<String>();

    public MainMenu(List<Service> services) {
        this.services = services;
        setMenuOptions();
    }

    public String getMenuTitle() {
        return menuTitle;
    }

    @Override
    public void setMenuOptions() {
        for (int i = 0; i < services.size(); i++) {
            menuOptions.add(services.get(i).getServiceDescription());
        }
    }

    @Override
    public void showMenu() {
        System.out.println(getMenuTitle());
        String menu = "";
        for (int i = 1; i < menuOptions.size() + 1; i++) {
            menu = menu + i + ". " + menuOptions.get(i - 1) + "\n";
        }
        System.out.println(menu);
    }

    public boolean showSpecificMenu(String input) {
        final String reg = "\\d+\\.{0,1}\\d*";
        int inputNum;
        if (input.matches(reg)) {
            inputNum = Integer.valueOf(input);
        } else {
            inputNum = Integer.MAX_VALUE;
        }
        if (inputNum <= menuOptions.size()) {
            Menu menu = services.get(inputNum - 1).getMenu();
            if (menu != null) menu.showMenu();
            return true;
        } else {
            showInvalidMessage();
            return false;
        }
    }

    public void showInvalidMessage() {
        System.out.println(invalidMessage);
    }
}